package me.zhengjie.pm.taskprogress.domain;

import com.alipay.api.domain.WorkDetail;
import javafx.concurrent.Task;
import lombok.Data;
import me.zhengjie.pm.taskprogress.service.dto.TaskProgressDto;

import java.io.Serializable;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class WorkhoursBase  implements Serializable {

    //id
    private int id;

    //员工工号
    private int workerId;

    //员工名称
    private String workerName;

    //中心名称
    private String centerName;

    //所内工作天数
    private int localWorkDays;

    //所内工作时长
    private int localWorkTime;

    //加班时长--小时
    private int overTime;

    //出差工作天数
    private int outerWorkDays;

    //出差工作工时
    private int outerWorkTime;

    //出差工时系数
    private float outerWorkStrengthNum;

    //总工时
    private int totalWorkhours;

    //本统计开始日期
    private Timestamp beginTime;

    //本统计结束日期
    private Timestamp endTime;

    //统计结果，各个项目，各个工作类型的分别用时
    //外层key = 项目编号  value为具体工作详情，这里取主要一个项目内部7类工作分别的用时及项目基本信息
    private Map<String,WorkhoursDetail> workDetailsMap = new HashMap<>();

    //本统计周期内相关的周报信息
    private List<TaskProgressDto> taskProgressDtos = new ArrayList<>();
    //根据workDetails计算其余统计数据
    public void dataStatistics(){
        //所内工作天数
        this.localWorkDays = 0;
        //加班时长--小时
        this.overTime = 0;
        //出差工作工时
        this.outerWorkTime = 0;
        //出差工时系数
        this.outerWorkStrengthNum = 1;
        //总工时
        this.totalWorkhours = 0;
        //清空
        this.workDetailsMap.clear();
        //统计
        for (int i = 0 ; i < this.taskProgressDtos.size();i++){
            TaskProgressDto temp = this.taskProgressDtos.get(i);

            String projectName = "";
            String projectNo = "";
            if (temp.getStaffWorkTask().getProjectTask() != null)
            {
                projectName = (temp.getStaffWorkTask().getProjectTask().getProjectBase().getProjectName());
                projectNo = (temp.getStaffWorkTask().getProjectTask().getProjectBase().getProjectNo());
            }
            if (temp.getStaffWorkTask().getProjectIssue() != null){
                projectName = (temp.getStaffWorkTask().getProjectIssue().getProjectBase().getProjectName());
                projectNo = (temp.getStaffWorkTask().getProjectIssue().getProjectBase().getProjectNo());
            }
            if (projectNo.length() == 0){
                //若未正确获取项目编号则continue
                continue;
            }
            if (workDetailsMap.containsKey(projectNo)){
                WorkhoursDetail tempDetail = workDetailsMap.get(projectNo);
                //工作类型，工时统计
                tempDetail.addWorkTypeHours(temp.getWorkType(),temp.getTimeUse().intValue(),temp.getAddress());
                workDetailsMap.put(projectNo,tempDetail);
            }else {
                WorkhoursDetail tempDetail = new WorkhoursDetail();
                //新增时，填写一些基本信息
                tempDetail.setId(temp.getId());
                //项目划分分为  项目---项目阶段----子任务/问题单  3层结构
                //根据项目阶段找出项目信息,所以必须确保项目都至少有一个阶段，每个分配的子任务都有其对应项目阶段
                //根据StaffWorkTask找出员工信息
                tempDetail.setTaskNo(projectNo);
                tempDetail.setTaskName(projectName);
                tempDetail.setWorkerId(temp.getStaffWorkTask().getWorkerId());
                tempDetail.setWorkerName(temp.getStaffWorkTask().getEmployeeBase().getEmpName());
                //工作类型，工时统计
                tempDetail.addWorkTypeHours(temp.getWorkType(),temp.getTimeUse().intValue(),temp.getAddress());
                workDetailsMap.put(projectNo,tempDetail);
            }
            //单位为小时
            if (temp.getAddress() == "所内"){
                this.localWorkTime += temp.getTimeUse().intValue();
            }else {
                this.outerWorkTime += temp.getTimeUse().intValue();
            }
        }
        //出差所内都暂时按8小时/天计算，不满一天部分四舍五入
        this.outerWorkDays = Math.round((float)this.outerWorkTime /8);
        this.localWorkDays = Math.round((float)this.localWorkTime /8);
        this.totalWorkhours = this.outerWorkTime +this.localWorkTime;
        //总加班时长暂时不统计
        this.overTime = 0;
    };

    //
    public void addTaskProgressDetails(TaskProgressDto d){

        this.taskProgressDtos.add(d);
    }
}
